package com.empire.module.member.controller.admin.warninggatherperson;

import com.empire.module.member.dal.dataobject.user.MemberUserDO;
import com.empire.module.member.dal.mysql.warninggatherperson.WarningGatherPersonMapper;
import com.empire.module.member.service.user.MemberUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.warninggatherperson.vo.*;
import com.empire.module.member.dal.dataobject.warninggatherperson.WarningGatherPersonDO;
import com.empire.module.member.service.warninggatherperson.WarningGatherPersonService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 聚集预警参与人员关联表（1条记录对应1人参与1次聚集）")
@RestController
@RequestMapping("/member/warning-gather-person")
@Validated
public class WarningGatherPersonController {

    @Resource
    private WarningGatherPersonService warningGatherPersonService;
    @Resource
    private MemberUserService memberUserService;

    @PostMapping("/create")
    @Operation(summary = "创建聚集预警参与人员关联表（1条记录对应1人参与1次聚集）")
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:create')")
    public CommonResult<Long> createWarningGatherPerson(@Valid @RequestBody WarningGatherPersonSaveReqVO createReqVO) {
        return success(warningGatherPersonService.createWarningGatherPerson(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新聚集预警参与人员关联表（1条记录对应1人参与1次聚集）")
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:update')")
    public CommonResult<Boolean> updateWarningGatherPerson(@Valid @RequestBody WarningGatherPersonSaveReqVO updateReqVO) {
        warningGatherPersonService.updateWarningGatherPerson(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除聚集预警参与人员关联表（1条记录对应1人参与1次聚集）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:delete')")
    public CommonResult<Boolean> deleteWarningGatherPerson(@RequestParam("id") Long id) {
        warningGatherPersonService.deleteWarningGatherPerson(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得聚集预警参与人员关联表（1条记录对应1人参与1次聚集）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:query')")
    public CommonResult<WarningGatherPersonRespVO> getWarningGatherPerson(@RequestParam("id") Long id) {
        WarningGatherPersonDO warningGatherPerson = warningGatherPersonService.getWarningGatherPerson(id);
        return success(BeanUtils.toBean(warningGatherPerson, WarningGatherPersonRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得聚集预警参与人员关联表（1条记录对应1人参与1次聚集）分页")
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:query')")
    public CommonResult<PageResult<WarningGatherPersonRespVO>> getWarningGatherPersonPage(@Valid WarningGatherPersonPageReqVO pageReqVO) {
        PageResult<WarningGatherPersonDO> pageResult = warningGatherPersonService.getWarningGatherPersonPage(pageReqVO);
        PageResult<WarningGatherPersonRespVO> voPageResult = BeanUtils.toBean(pageResult, WarningGatherPersonRespVO.class);
        List<WarningGatherPersonRespVO> list = voPageResult.getList();
        for (WarningGatherPersonRespVO vo : list) {
            MemberUserDO user = memberUserService.getUser(vo.getPersonId());
            vo.setPersonName(user.getName());
            Integer foreigner = user.getForeigner();
            if (foreigner == 1) {
                vo.setIdNumber(user.getPassportNumber()+"(护照号)");
            }
            if (foreigner == 2) {
                vo.setIdNumber(user.getIdcard()+"(身份证号)");
            }
        }
        return success(voPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出聚集预警参与人员关联表（1条记录对应1人参与1次聚集） Excel")
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarningGatherPersonExcel(@Valid WarningGatherPersonPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarningGatherPersonDO> list = warningGatherPersonService.getWarningGatherPersonPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "聚集预警参与人员关联表（1条记录对应1人参与1次聚集）.xls", "数据", WarningGatherPersonRespVO.class,
                        BeanUtils.toBean(list, WarningGatherPersonRespVO.class));
    }

    /**
     * 单条标记已读
     * @param id 聚集人员记录ID
     */
    @PutMapping("/mark-single-read")
    @Operation(summary = "单条标记已读")
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:update')") // 复用更新权限
    public CommonResult<Boolean> markSingleRead(@RequestParam("id") Long id) {
        warningGatherPersonService.markSingleRead(id);
        return success(true);
    }

    /**
     * 批量标记已读
     * @param ids 聚集人员记录ID列表
     */
    @PutMapping("/mark-batch-read")
    @Operation(summary = "批量标记已读")
    @PreAuthorize("@ss.hasPermission('member:warning-gather-person:update')") // 复用更新权限
    public CommonResult<Boolean> markBatchRead(@RequestBody List<Long> ids) { // 接收ID列表
        warningGatherPersonService.markBatchRead(ids);
        return success(true);
    }

}